﻿#include "TestReport.h"
#include "DatabaseMgmt.h"

CTestReport::CTestReport()
{

}

CTestReport::~CTestReport()
{

}

int CTestReport::insertRow(TestReport report, uint32_t& uid)
{
	int ret = CONST_ERR_SUCCESS;

	QString strSql = QString("INSERT INTO TB_TEST_REPORT (NAME, REPORT_CODE, REPORT_DATE, SAMPLE_CODE,"
		" CLIENTOR, DECLARATION_NAME, GOODS_NAME, FROM_REGION, SAMPLE_WAY, BILL_ENTRY, SAMPLE_DATE, SAMPLE_SEAL_NO," 
		" BEGIN_DATE, END_DATE, SAMPLE_COUNT) VALUES ('%1','%2','%3','%4','%5','%6','%7','%8','%9','%10',"
		" '%11','%12','%13','%14',%15);")
		.arg(report.name)
		.arg(report.report_code)
		.arg(report.report_date.toString("yyyy-MM-dd"))
		.arg(report.sample_code)
		.arg(report.clientor)
		.arg(report.declaration_name)
		.arg(report.goods_name)
		.arg(report.from_region)
		.arg(report.sample_way)
		.arg(report.bill_entry)
		.arg(report.sample_date.toString("yyyy-MM-dd"))
		.arg(report.sample_seal_no)
		.arg(report.begin_date.toString("yyyy-MM-dd"))
		.arg(report.end_date.toString("yyyy-MM-dd"))
		.arg(report.sample_count);

	QSqlQuery query;
	CDatabaseMgmt::getInstance()->execSql(strSql, query);

	if (query.lastError().type() != QSqlError::NoError)
	{
		qInfo() << query.lastError().text();
		ret = CONST_ERR_EXEC_SQL_FAILED;
	}
	else
	{
		strSql = " SELECT LAST_INSERT_ROWID() FROM [TB_TEST_REPORT]; ";
		CDatabaseMgmt::getInstance()->execSql(strSql, query);

		if (query.lastError().type() != QSqlError::NoError)
		{
			ret = CONST_ERR_EXEC_SQL_FAILED;
			qInfo() << query.lastError().text();
		}
		else
		{
			if (query.next())
			{
				uid = query.value(0).toUInt();
			}
			else
			{
				uid = 0;
				qInfo() << query.lastError().text();
			}
		}		
	}
	return ret;
}

int CTestReport::removeRow(int id)
{
	int ret = CONST_ERR_SUCCESS;

	QString strSql = QString("DELETE FROM TB_TEST_REPORT WHERE UID=%1")
		.arg(id);

	QSqlQuery query;
	CDatabaseMgmt::getInstance()->execSql(strSql, query);

	if (query.lastError().type() != QSqlError::NoError)
	{
		ret = CONST_ERR_EXEC_SQL_FAILED;
		qInfo() << query.lastError().text();
	}

	return ret;
}

int CTestReport::modifyRow(TestReport report)
{
	int ret = CONST_ERR_SUCCESS;

	QString strSql = QString("UPDATE TB_TEST_REPORT SET NAME='%1', REPORT_CODE='%2',"
		" REPORT_DATE='%3', SAMPLE_CODE='%4',"
		" CLIENTOR='%5', DECLARATION_NAME='%6', GOODS_NAME='%7', FROM_REGION='%8', SAMPLE_WAY='%9',"
		" BILL_ENTRY='%10', SAMPLE_DATE='%11', SAMPLE_SEAL_NO='%12',"
		" BEGIN_DATE='%13', END_DATE='%14', SAMPLE_COUNT=%15 WHERE UID=%16")
		.arg(report.name)
		.arg(report.report_code)
		.arg(report.report_date.toString("yyyy-MM-dd"))
		.arg(report.sample_code)
		.arg(report.clientor)
		.arg(report.declaration_name)
		.arg(report.goods_name)
		.arg(report.from_region)
		.arg(report.sample_way)
		.arg(report.bill_entry)
		.arg(report.sample_date.toString("yyyy-MM-dd"))
		.arg(report.sample_seal_no)
		.arg(report.begin_date.toString("yyyy-MM-dd"))
		.arg(report.end_date.toString("yyyy-MM-dd"))
		.arg(report.sample_count)
		.arg(report.uid);

	QSqlQuery query;
	CDatabaseMgmt::getInstance()->execSql(strSql, query);

	if (query.lastError().type() != QSqlError::NoError)
	{
		ret = CONST_ERR_EXEC_SQL_FAILED;
		qInfo() << query.lastError().text();
	}

	return ret;
}

int CTestReport::queryById(uint32_t uid, TestReport& report)
{
	int ret = CONST_ERR_SUCCESS;
	QString strSql = QString("SELECT NAME, REPORT_CODE, REPORT_DATE, SAMPLE_CODE,"
		" CLIENTOR, DECLARATION_NAME, GOODS_NAME, FROM_REGION, SAMPLE_WAY, BILL_ENTRY,"
		" SAMPLE_DATE, SAMPLE_SEAL_NO, BEGIN_DATE, END_DATE, SAMPLE_COUNT"
		" FROM TB_TEST_REPORT WHERE UID=%1")
		.arg(uid);

	QSqlQuery query;
	CDatabaseMgmt::getInstance()->execSql(strSql, query);

	if (query.lastError().type() != QSqlError::NoError)
	{
		ret = CONST_ERR_EXEC_SQL_FAILED;
		qInfo() << query.lastError().text();
	}
	else
	{
		if (query.next())
		{
			report.uid = uid;
			report.name = query.value("NAME").toString();
			report.report_code = query.value("REPORT_CODE").toString();
			report.report_date = query.value("REPORT_DATE").toDate();
			report.sample_code = query.value("SAMPLE_CODE").toString();
			report.clientor = query.value("CLIENTOR").toString();
			report.declaration_name = query.value("DECLARATION_NAME").toString();
			report.goods_name = query.value("GOODS_NAME").toString();
			report.from_region = query.value("FROM_REGION").toString();
			report.sample_way = query.value("SAMPLE_WAY").toString();
			report.bill_entry = query.value("BILL_ENTRY").toString();
			report.sample_date = query.value("SAMPLE_DATE").toDate();
			report.sample_seal_no = query.value("SAMPLE_SEAL_NO").toString();
			report.begin_date = query.value("BEGIN_DATE").toDate();
			report.end_date = query.value("END_DATE").toDate();
			report.sample_count = query.value("SAMPLE_COUNT").toInt();
		}
	}
	return ret;
}

int CTestReport::queryAll(QVector<TestReport>& reports)
{
	int ret = CONST_ERR_SUCCESS;

	QString strSql = QString("SELECT UID, NAME, REPORT_CODE, REPORT_DATE, SAMPLE_CODE,"
		" CLIENTOR, DECLARATION_NAME, GOODS_NAME, FROM_REGION, SAMPLE_WAY, BILL_ENTRY," 
		" SAMPLE_DATE, SAMPLE_SEAL_NO, BEGIN_DATE, END_DATE,SAMPLE_COUNT"
		" FROM TB_TEST_REPORT");

	QSqlQuery query;
	CDatabaseMgmt::getInstance()->execSql(strSql, query);

	if (query.lastError().type() != QSqlError::NoError)
	{
		ret = CONST_ERR_EXEC_SQL_FAILED;
		qInfo() << query.lastError().text();
	}
	else
	{
		while (query.next())
		{
			TestReport row;
			row.uid = query.value("UID").toUInt();
			row.name = query.value("NAME").toString();
			row.report_code = query.value("REPORT_CODE").toString();
			row.report_date = query.value("REPORT_DATE").toDate();
			row.sample_code = query.value("SAMPLE_CODE").toString();
			row.clientor = query.value("CLIENTOR").toString();
			row.declaration_name = query.value("DECLARATION_NAME").toString();
			row.goods_name = query.value("GOODS_NAME").toString();
			row.from_region = query.value("FROM_REGION").toString();
			row.sample_way = query.value("SAMPLE_WAY").toString();
			row.bill_entry = query.value("BILL_ENTRY").toString();
			row.sample_date = query.value("SAMPLE_DATE").toDate();
			row.sample_seal_no = query.value("SAMPLE_SEAL_NO").toString();
			row.begin_date = query.value("BEGIN_DATE").toDate();
			row.end_date = query.value("END_DATE").toDate();
			row.sample_count = query.value("SAMPLE_COUNT").toInt();
			reports.append(row);
		}
	}

	return ret;
}
